package com.example.smartall.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.smartall.R;
import com.example.smartall.adapter.MineListAdapter;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.diaglog.NetSetDialog;
import com.example.smartall.fragment.base.BaseFragment;
import com.example.smartall.ui.ChangePasswordActivity;
import com.example.smartall.ui.FeedbackActivity;
import com.example.smartall.ui.HospitalActivity;
import com.example.smartall.ui.LoginActivity;
import com.example.smartall.ui.NewsActivity;
import com.example.smartall.ui.OrderActivity;
import com.example.smartall.ui.OrderListActivity;
import com.example.smartall.ui.UserInfoActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

 /**
  * @Author:         abbas
  * @Description:    个人中心
  *
  * （1）首先进入个人中心页面，个人中心页面显示用户头像、账户、个人信息页面入口、订单列表页面入口、修改密码页面入口、意见反馈页面入口，点击“退出”按钮可退出登录。
  * （2）点击个人信息跳转至个人信息页面，标签栏显示本页面标题，点击返回图标可返回到上一页，点击修改可保存修改的信息，可修改内容为：头像、昵称、性别、联系电话，注：证件号只显示前两位与后四位数字其他使用 * 号代替。
  * （3）点击修改密码可进入修改密码页面，标签栏显示本页面标题，点击返回图标可返回到上一页，输入原密码与新密码，点击“确定”按钮可保存修改的信息。
  * （4）点击订单列表可跳转到订单页面，标签栏显示本页面标题，点击返回图标可返回到上一页，页面内容展示所有订单、订单分类数据信息，订单显示信息有：订单号、订单类型、订单生成日期。点击订单可跳转至对应功能模块生成订单的详情页面。
  * （5）点击意见反馈可跳转至意见反馈页面，标签栏显示本页面标题，点击返回图标可返回到上一页，输入反馈的内容，字数限制在150字以内，点击提交可提交反馈的意见。
  */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    private ImageView pic;
    private TextView userNick;
    private TextView userName;
    private Button btLogin,btLayout;
    private Button btResetNet;
    private Button btNews;
    private Button btOrder;
    private Button btPervasive;

    private ListView lvMine;
    private MineListAdapter listAdapter;
    private SPUtils spUtils;

    private static String[] listData;

    private HashMap<Integer,Class> clickHandlerMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        //列表数据 自己瞄
        listData = getActivity().getResources().getStringArray(R.array.MineList);

        init(view);
        initEvent();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

     @Override
     public void onResume() {
         super.onResume();
         if (clickHandlerMap == null  || clickHandlerMap.size() == 0){
             clickHandlerMap = new HashMap<>();
             clickHandlerMap.put(0,UserInfoActivity.class);
             clickHandlerMap.put(1,OrderListActivity.class);
             clickHandlerMap.put(2,ChangePasswordActivity.class);
             clickHandlerMap.put(3,FeedbackActivity.class);
         }
     }

     private void init(View view) {
        pic = (ImageView) view.findViewById(R.id.pic);
        userNick = (TextView) view.findViewById(R.id.userNick);
        userName = (TextView) view.findViewById(R.id.userName);
        btLogin = (Button) view.findViewById(R.id.bt_login);
        lvMine = (ListView) view.findViewById(R.id.lv_mine);
        btLayout = (Button) view.findViewById(R.id.layout);
        btResetNet = (Button) view.findViewById(R.id.bt_resetNet);

        //新闻模块
        btNews = (Button) view.findViewById(R.id.bt_news);
        btNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), NewsActivity.class));
            }
        });
        //外卖模块
        btOrder = (Button) view.findViewById(R.id.bt_order);
        btOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), OrderActivity.class));
            }
        });
        //门诊模块
        btPervasive = view.findViewById(R.id.bt_pervasive);
        btPervasive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), HospitalActivity.class));
            }
        });
        listAdapter = new MineListAdapter(getActivity(), Arrays.asList(listData));
        lvMine.setAdapter(listAdapter);
        initData();
    }

    private void initData() {
        if (spUtils == null){
            spUtils = SPUtils.config(getActivity(), Constant.SP_USER_FILE_NAME);
        }

        //若有token即为已经登录
        if (spUtils.hasToken()){
            btLogin.setVisibility(View.GONE);
            Glide.with(getActivity())
                    .load(ApiConfig.BASE_URL + spUtils.getString(Constant.USER_AVATAR))
                    .transform(new CircleCrop())
                    .into(pic);
            userNick.setText("".equals(spUtils.getString(Constant.USER_NICKNAME))? "尚无昵称" : spUtils.getString(Constant.USER_NICKNAME));
            userName.setText(spUtils.getString(Constant.USER_NAME));
            btLayout.setVisibility(View.VISIBLE);
        }else{
            btLogin.setVisibility(View.VISIBLE);
            Glide.with(getActivity())
                    .load(R.mipmap.ic_launcher_round)
                    .transform(new CircleCrop())
                    .into(pic);
            userNick.setText("");
            userName.setText("");
            btLayout.setVisibility(View.GONE);
        }
    }

    private void initEvent() {
        btLogin.setOnClickListener(this);
        btLayout.setOnClickListener(this);
        btResetNet.setOnClickListener(this);

        lvMine.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (spUtils.hasToken()){
                    if (clickHandlerMap.containsKey(i)){
                        migrateTo(clickHandlerMap.get(i));
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //登录
            case R.id.bt_login:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.layout:
                //退出登录
                spUtils.clearAll();
                initData();
                showToast("已退出登录");
                break;
            case R.id.bt_resetNet:
                //网络端口ip设置
                NetSetDialog dialog = new NetSetDialog(getActivity());
                dialog.show();
                break;
        }
    }
}